package org.xiyu.yee.onekeyminer_chainable_visual;

import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.event.config.ModConfigEvent;
import net.neoforged.neoforge.common.ModConfigSpec;

import java.util.List;

/**
 * OneKeyMiner Chainable Visual Configuration
 * 支持NeoForge原生配置GUI和颜色调色盘
 */
@EventBusSubscriber(modid = Onekeyminer_chainable_visual.MODID, bus = EventBusSubscriber.Bus.MOD)
public class Config {
    private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();

    // ==================== 显示设置分类 ====================
    static {
        BUILDER.comment("Display Settings - Control what chain highlights are shown")
                .push("display");
    }
    
    private static final ModConfigSpec.BooleanValue SHOW_MINING = BUILDER
            .comment("Show mining chain block positions as highlighted outlines")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.showMining")
            .define("showMining", true);
            
    private static final ModConfigSpec.BooleanValue SHOW_INTERACT = BUILDER
            .comment("Show interaction chain block positions as highlighted outlines")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.showInteraction")
            .define("showInteraction", true);
    
    private static final ModConfigSpec.BooleanValue OUTLINE_MODE = BUILDER
            .comment("If enabled: render thick outlines. If disabled: render thin wireframes")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.outlineMode")
            .define("outlineMode", false);
    
    private static final ModConfigSpec.BooleanValue SHOW_THROUGH = BUILDER
            .comment("Render outlines through blocks (X-ray vision)")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.showThroughWalls")
            .define("showThroughWalls", true);
    
    private static final ModConfigSpec.DoubleValue LINE_ALPHA = BUILDER
            .comment("Multiplier for line transparency (0.0 = invisible, 1.0 = opaque)")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.lineAlpha")
            .defineInRange("lineAlpha", 1.0D, 0.0D, 1.0D);
    
    private static final ModConfigSpec.DoubleValue HATCH_SPACING = BUILDER
            .comment("Spacing between hatch lines in block units")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.hatchSpacing")
            .defineInRange("hatchSpacing", 0.25D, 0.05D, 1.0D);
    
    private static final ModConfigSpec.BooleanValue SHEARING_ENABLE = BUILDER
            .comment("Enable glowing highlight for sheep when holding shears")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.shearingEnable")
            .define("shearingEnable", true);

    private static final ModConfigSpec.DoubleValue SHEARING_RANGE = BUILDER
        .comment("Search radius around player for highlighting chain-shearing sheep when crosshair targets a sheep")
        .translation("text.autoconfig.onekeyminer_chainable_visual.option.shearingRange")
        .defineInRange("shearingRange", 48.0D, 4.0D, 128.0D);
    
    static { BUILDER.pop(); } // 结束display分类

    // ==================== 颜色设置分类 ====================
    static {
        BUILDER.comment("Color Settings - Configure highlight colors with RGBA values (0.0-1.0)")
                .push("colors");
    }
    
    private static final ModConfigSpec.ConfigValue<List<? extends Double>> COLOR_MINING_CFG = BUILDER
            .comment("RGBA color for mining chain highlights", "Format: [Red, Green, Blue, Alpha] (0.0-1.0 each)")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.colorMining")
            .defineListAllowEmpty("colorMining", 
                    List.of(0.0D, 1.0D, 0.0D, 0.45D), 
                    o -> o instanceof Double && ((Double)o) >= 0.0D && ((Double)o) <= 1.0D);
    
    private static final ModConfigSpec.ConfigValue<List<? extends Double>> COLOR_INTERACT_CFG = BUILDER
            .comment("RGBA color for interaction chain highlights", "Format: [Red, Green, Blue, Alpha] (0.0-1.0 each)")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.colorInteract")
            .defineListAllowEmpty("colorInteract", 
                    List.of(0.0D, 0.5D, 1.0D, 0.45D), 
                    o -> o instanceof Double && ((Double)o) >= 0.0D && ((Double)o) <= 1.0D);
    
    private static final ModConfigSpec.ConfigValue<List<? extends Double>> COLOR_SHEARING_CFG = BUILDER
            .comment("RGBA color for sheep glow effect", "Format: [Red, Green, Blue, Alpha] (0.0-1.0 each)")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.colorShearing")
            .defineListAllowEmpty("colorShearing", 
                    List.of(1.0D, 1.0D, 1.0D, 1.0D), 
                    o -> o instanceof Double && ((Double)o) >= 0.0D && ((Double)o) <= 1.0D);
    
    static { BUILDER.pop(); } // 结束colors分类

    // ==================== 高级设置分类 ====================
    static {
        BUILDER.comment("Advanced Settings - Technical rendering options")
                .push("advanced");
    }
    
    private static final ModConfigSpec.BooleanValue USE_CUSTOM_RENDER_TYPES = BUILDER
            .comment("Enable custom rendering pipelines for better visual effects", 
                     "Disable if experiencing rendering issues or compatibility problems")
            .translation("text.autoconfig.onekeyminer_chainable_visual.option.useCustomRenderTypes")
            .define("useCustomRenderTypes", true);
    
    static { BUILDER.pop(); } // 结束advanced分类

    static final ModConfigSpec SPEC = BUILDER.build();

    // ==================== 运行时配置值 ====================
    public static boolean showMining;
    public static boolean showInteraction;
    public static boolean outlineMode;
    public static boolean showThroughWalls;
    public static double lineAlpha;
    public static double hatchSpacing;
    public static boolean shearingEnable;
    public static double shearingRange;
    public static boolean useCustomRenderTypes;
    
    // 颜色配置（RGBA 0.0-1.0）
    public static float[] colorMining = {0.0f, 1.0f, 0.0f, 0.45f};
    public static float[] colorInteract = {0.0f, 0.5f, 1.0f, 0.45f};
    public static float[] colorShearing = {1.0f, 1.0f, 1.0f, 1.0f};

    // ==================== 配置事件处理 ====================
    @SubscribeEvent
    static void onLoad(final ModConfigEvent event) {
        // 加载显示设置
        showMining = SHOW_MINING.get();
        showInteraction = SHOW_INTERACT.get();
        outlineMode = OUTLINE_MODE.get();
        showThroughWalls = SHOW_THROUGH.get();
        lineAlpha = LINE_ALPHA.get();
        hatchSpacing = HATCH_SPACING.get();
        shearingEnable = SHEARING_ENABLE.get();
    shearingRange = SHEARING_RANGE.get();
        useCustomRenderTypes = USE_CUSTOM_RENDER_TYPES.get();

        // 加载颜色设置
        loadColorArray(COLOR_MINING_CFG.get(), colorMining);
        loadColorArray(COLOR_INTERACT_CFG.get(), colorInteract);
        loadColorArray(COLOR_SHEARING_CFG.get(), colorShearing);
    }

    /**
     * 安全地加载颜色数组配置
     * @param configList 配置文件中的颜色值列表
     * @param targetArray 目标颜色数组 [r,g,b,a]
     */
    private static void loadColorArray(List<? extends Double> configList, float[] targetArray) {
        if (configList.size() >= 4) {
            for (int i = 0; i < 4; i++) {
                double value = configList.get(i);
                targetArray[i] = (float) Math.max(0.0, Math.min(1.0, value));
            }
        }
    }

    // ==================== 便民获取方法 ====================
    
    /**
     * 获取挖掘高亮颜色
     * @return RGBA颜色数组
     */
    public static float[] getMiningColor() {
        return colorMining.clone();
    }

    /**
     * 获取交互高亮颜色
     * @return RGBA颜色数组
     */
    public static float[] getInteractionColor() {
        return colorInteract.clone();
    }

    /**
     * 获取剪羊毛发光颜色
     * @return RGBA颜色数组
     */
    public static float[] getShearingColor() {
        return colorShearing.clone();
    }

    /**
     * 是否显示挖掘高亮
     */
    public static boolean isShowMining() {
        return showMining;
    }

    /**
     * 是否显示交互高亮
     */
    public static boolean isShowInteraction() {
        return showInteraction;
    }

    /**
     * 是否启用轮廓模式
     */
    public static boolean isOutlineMode() {
        return outlineMode;
    }

    /**
     * 是否穿墙显示
     */
    public static boolean isShowThroughWalls() {
        return showThroughWalls;
    }

    /**
     * 获取线条透明度倍数
     */
    public static double getLineAlpha() {
        return lineAlpha;
    }

    /**
     * 获取填充线间距
     */
    public static double getHatchSpacing() {
        return hatchSpacing;
    }

    /**
     * 是否启用剪羊毛高亮
     */
    public static boolean isShearingEnabled() {
        return shearingEnable;
    }

    public static double getShearingRange() { return shearingRange; }

    /**
     * 是否使用自定义渲染类型
     */
    public static boolean useCustomRenderTypes() {
        return useCustomRenderTypes;
    }
}
